home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / graphics_libraries / text / MeasureText.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  9KB  |  243 lines

  1. ;/* MeasureText - Execute me to compile me with Lattice 5.10a
  2. LC -b0 -cfistq -v -y -j73 MeasureText.c
  3. Blink FROM LIB:c.o, MeasureText.o TO MeasureText LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. #define INTUITION_IOBSOLETE_H
  8. #include <dos/dos.h>
  9. #include <dos/dosextens.h>
  10. #include <graphics/text.h>
  11. #include <graphics/rastport.h>
  12. #include <intuition/intuition.h>
  13. #include <exec/libraries.h>
  14. #include <clib/alib_stdio_protos.h>
  15. #include <clib/graphics_protos.h>
  16. #include <clib/intuition_protos.h>
  17. #include <clib/diskfont_protos.h>
  18. #include <clib/dos_protos.h>
  19. #include <clib/exec_protos.h>
  20. #include <clib/asl_protos.h>
  21.  
  22. #ifdef LATTICE
  23. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  24. int chkabort(void) { return(0); }
  25. #endif
  26.  
  27. #define BUFSIZE 32768
  28.  
  29. UBYTE *vers = "\0$VER: MeasureText 37.1";
  30.  
  31. UBYTE buffer[BUFSIZE];
  32.  
  33. void MainLoop(void);
  34. void EOP(void);
  35.  
  36. struct Library *IntuitionBase;
  37. struct Library *GfxBase;
  38. struct Library *DiskfontBase;
  39. struct Library *AslBase;
  40.  
  41. BPTR myfile;
  42. UWORD wtbarheight;
  43. struct FontRequester *fr;
  44. struct TextFont *myfont;
  45. struct Window *w;
  46. struct RastPort *myrp;
  47. struct Task *mytask;
  48.  
  49. void main(int argc, char **argv)
  50. {
  51.   struct TextAttr myta;
  52.   extern struct Library *DOSBase;
  53.  
  54.   if (DOSBase->lib_Version >35)  /* Run only on 2.0 machines */
  55.   {
  56.     if (argc == 2)
  57.     {
  58.       if (myfile = Open(argv[1], MODE_OLDFILE))                     /* Open the file to print out. */
  59.       {
  60.         if (DiskfontBase = OpenLibrary("diskfont.library", 37L))            /* Open the libraries. */
  61.         {
  62.           if (IntuitionBase = OpenLibrary("intuition.library", 37L))
  63.           {
  64.             if (GfxBase = OpenLibrary("graphics.library", 37L))
  65.             {
  66.               if (AslBase = OpenLibrary("asl.library", 37L))
  67.               {
  68.                 if (fr = (struct FontRequester *)                    /* Open an ASL font requester */
  69.                          AllocAslRequestTags(ASL_FontRequest,
  70.                                       /* Supply initial values for requester */
  71.                                       ASL_FontName, (ULONG)"topaz.font",
  72.                                       ASL_FontHeight, 11L,
  73.                                       ASL_FontStyles, FSF_BOLD | FSF_ITALIC,
  74.                                       ASL_FrontPen,  0x01L,
  75.                                       ASL_BackPen,   0x00L,
  76.  
  77.                                        /* Give us all the gadgetry */
  78.                                       ASL_FuncFlags, FONF_FRONTCOLOR | FONF_BACKCOLOR |
  79.                                                      FONF_DRAWMODE | FONF_STYLES,
  80.                                       TAG_DONE))
  81.                 {
  82.                   /* Pop up the requester */
  83.                   if (AslRequest(fr, 0L))
  84.                   {
  85.                     myta.ta_Name       = fr->fo_Attr.ta_Name;              /* extract the font and */
  86.                     myta.ta_YSize      = fr->fo_Attr.ta_YSize;             /* display attributes   */
  87.                     myta.ta_Style      = fr->fo_Attr.ta_Style;             /* from the FontRequest */
  88.                     myta.ta_Flags      = fr->fo_Attr.ta_Flags;             /* structure.           */
  89.  
  90.                     if (myfont = OpenDiskFont(&myta))
  91.                     {
  92.                       if (w = OpenWindowTags(NULL,WA_SizeGadget,  TRUE,
  93.                                                   WA_MinWidth,    200,
  94.                                                   WA_MinHeight,   200,
  95.                                                   WA_DragBar,     TRUE,
  96.                                                   WA_DepthGadget, TRUE,
  97.                                                   WA_Title,       (ULONG)argv[1],
  98.                                                   TAG_DONE))
  99.                       {
  100.                         myrp = w->RPort;
  101.                                  /* figure out where the baseline of the uppermost line should be. */
  102.                         wtbarheight = w->WScreen->BarHeight + myfont->tf_Baseline + 2;
  103.  
  104.                             /* Set the font and add software styling to the text if I asked for it */
  105.                             /* in OpenFont() and didn't get it.  Because most Amiga fonts do not   */
  106.                             /* have styling built into them (with the exception of the CG outline  */
  107.                             /* fonts), if the user selected some kind of styling for the text, it  */
  108.                             /* will to be added algorithmically by calling SetSoftStyle().         */
  109.  
  110.                         SetFont(myrp, myfont);
  111.                         SetSoftStyle(myrp,   myta.ta_Style ^ myfont->tf_Style,
  112.                                       (FSF_BOLD | FSF_UNDERLINED | FSF_ITALIC));
  113.                         SetDrMd(myrp, fr->fo_DrawMode);
  114.                         SetAPen(myrp, fr->fo_FrontPen);
  115.                         SetBPen(myrp, fr->fo_BackPen);
  116.                         Move(myrp, w->WScreen->WBorLeft, wtbarheight);
  117.                         mytask = FindTask(NULL);
  118.  
  119.                         MainLoop();
  120.  
  121.                         Delay(25);                         /* short delay to give user a chance to */
  122.                         CloseWindow(w);                   /* see the text before it goes away.    */
  123.                       }
  124.                       CloseFont(myfont);
  125.                     }
  126.                   }
  127.                   else
  128.                     VPrintf("Request Cancelled\n", NULL);
  129.                   FreeAslRequest(fr);
  130.                 }
  131.                 CloseLibrary(AslBase);
  132.               }
  133.               CloseLibrary(GfxBase);
  134.             }
  135.             CloseLibrary(IntuitionBase);
  136.           }
  137.           CloseLibrary(DiskfontBase);
  138.         }
  139.         Close(myfile);
  140.       }
  141.     }
  142.     else
  143.       VPrintf("template: MeasureText <file name>\n", NULL);
  144.   }
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. void MainLoop(void)
  155. {
  156.   struct TextExtent resulttextent;
  157.   LONG fit, actual, count, printable, crrts;
  158.   BOOL aok = TRUE;
  159.  
  160.   while (((actual = Read(myfile, buffer, BUFSIZE)) > 0) && aok)    /* while there's something to */
  161.   {                                                                /* read, fill the buffer.     */
  162.     count = 0;
  163.  
  164.     while(count < actual)
  165.     {
  166.       crrts = 0;
  167.  
  168.       while ( ((buffer[count] < myfont->tf_LoChar) ||      /* skip non-printable characters, but */
  169.                (buffer[count] > myfont->tf_HiChar)) &&     /* account for newline characters.    */
  170.                (count < actual) )
  171.       {
  172.         if (buffer[count] == '\012') crrts++;   /* is this character a newline?  if it is, bump */
  173.         count++;                                 /* up the newline count.                        */
  174.       }
  175.  
  176.       if (crrts > 0)                    /* if there where any newlines, be sure to display them. */
  177.       {
  178.         Move(myrp, w->BorderLeft, myrp->cp_y + (crrts * (myfont->tf_YSize + 1)));
  179.         EOP();                                            /* did we go past the end of the page? */
  180.       }
  181.  
  182.       printable = count;
  183.       while ( (buffer[printable] >= myfont->tf_LoChar) &&        /* find the next non-printables */
  184.               (buffer[printable] <= myfont->tf_HiChar) &&
  185.               (printable < actual) )
  186.       {
  187.         printable++;
  188.       }                                    /* print the string of printable characters wrapping  */
  189.       while (count < printable)            /* lines to the beginning of the next line as needed. */
  190.       {
  191.                                        /* how many characters in the current string of printable */
  192.         fit = TextFit(  myrp,                &(buffer[count]),/* characters will fit between the */
  193.                         (printable - count), &resulttextent,  /* rastport's current X position   */
  194.                         NULL,                1,               /* and the edge of the window?     */
  195.                         (w->Width  - (myrp->cp_x + w->BorderLeft + w->BorderRight)),
  196.                                              myfont->tf_YSize + 1  );
  197.         if ( fit == 0 )
  198.         {
  199.           Move(myrp, w->BorderLeft, myrp->cp_y + myfont->tf_YSize + 1); /* nothing else fits on  */
  200.         }                                           /* this line, need to wrap to the next line. */
  201.         else
  202.         {
  203.            Text(myrp, &(buffer[count]), fit);
  204.            count += fit;
  205.         }
  206.         EOP();
  207.       }
  208.  
  209.       if (mytask->tc_SigRecvd & SIGBREAKF_CTRL_C)          /* did the user hit CTRL-C (the shell */
  210.       {                                                    /* window has to receive the CTRL-C)? */
  211.         aok = FALSE;
  212.         VPrintf("Ctrl-C Break\n", NULL);
  213.         count = BUFSIZE + 1;
  214.       }
  215.     }
  216.   }
  217.   if (actual < 0)
  218.     VPrintf("Error while reading\n", NULL);
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. void EOP(void)
  231. {
  232.     if (myrp->cp_y > (w->Height - (w->BorderBottom + 2))) /* If we reached page bottom, clear the */
  233.     {                                                     /* rastport and move back to the top.   */
  234.         Delay(25);
  235.  
  236.         SetAPen(myrp, 0);
  237.         RectFill(myrp, (LONG)w->BorderLeft, (LONG)w->BorderTop, w->Width - (w->BorderRight + 1),
  238.                  w->Height - (w->BorderBottom + 1) );
  239.         SetAPen(myrp, 1);
  240.         Move(myrp, w->BorderLeft + 1, wtbarheight);
  241.         SetAPen(myrp, fr->fo_FrontPen);
  242.     }
  243. }